﻿(function ($) {

    $.fn.metro = function () {
        return this.each(function () {

            var $this = this;
            var mertoCenterAnimating = false;
            var metroCenterAnimatIndex = 0;
            var animMetroCenter = function (delta) {
                if (mertoCenterAnimating)
                    return;
                var animIndex;
                if (delta > 0) {
                    if (metroCenterAnimatIndex == 0) {
                        resetMetroCenterNavigate();
                        return;
                    }
                    animIndex = metroCenterAnimatIndex - 1;
                    if (!$.fn.metro.onPrevClick(animIndex)) { return; }
                    metroCenterAnimatIndex = animIndex;
                }
                else if (delta < 0) {
                    if (metroCenterAnimatIndex == $('.metro_center_part', $this).length - 1) {
                        resetMetroCenterNavigate();
                        return;
                    }
                    animIndex = metroCenterAnimatIndex + 1;
                    if (!$.fn.metro.onNextClick(animIndex)) { return; }
                    metroCenterAnimatIndex = animIndex;
                }

                var metroCenterWrapperPosX = 0;
                for (var i = 0; i < metroCenterAnimatIndex; i++) {
                    metroCenterWrapperPosX += $($('.metro_center_part', $this)[i]).outerWidth();
                }

                mertoCenterAnimating = true;
                $('.metro_center_navigate', $this).hide();
                $('.metro_center_wrapper_contain', $this).animate({ left: metroCenterWrapperPosX * -1 }, 300, function () {
                    mertoCenterAnimating = false;
                    resetMetroCenterNavigate();
                });
            };
            var resetMetroCenterNavigate = function () {
                if (metroCenterAnimatIndex == 0) {
                    $('.metro_center_navigate.left', $this).hide();
                }
                else {
                    $('.metro_center_navigate.left', $this).show();
                }

                if (metroCenterAnimatIndex == $('.metro_center_part', $this).length - 1) {
                    $('.metro_center_navigate.right', $this).hide();
                }
                else {
                    $('.metro_center_navigate.right', $this).show();
                }

                $('.metro_center_navigate.right', $this).css({ left: $($('.metro_center_part', $this)[metroCenterAnimatIndex]).outerWidth() + 12 });
            };
            var initMetroCenter = function () {
                //set metro center wrapper width
                var metro_center_wrapper_width = 0;
                $('.metro_center_part', $this).each(function (index) {
                    metro_center_wrapper_width += $(this).outerWidth();
                });
                $('.metro_center_wrapper_contain', $this).width(metro_center_wrapper_width);

                //set part line clear
                $(".part_line", $this).each(function (index) {
                    var content = '<div class="clear"></div>';
                    $(this).append(content);
                    $(this).prepend(content);
                });

                //set mouse wheel
                $(document).unbind('mousewheel').bind('mousewheel', function (event, delta) {
                    animMetroCenter(delta);
                });
                $('.metro_center_navigate.left', $this).unbind('click').bind('click', function () {
                    animMetroCenter(1);
                });
                $('.metro_center_navigate.right', $this).unbind('click').bind('click', function () {
                    animMetroCenter(-1);
                });
                $('.metro_sub_center_navigate.left', $this).unbind('click').bind('click', function () {
                    $.fn.metro.onPrevClick(-1);
                });
            }
            var initMetroSide = function () {
                $('.metro_side.bottom', $this).unbind('mouseleave').bind('mouseleave', function () {
                    $(this).slideUp(300);
                });
                $('.metro_side.bottom', $this).mouseleave();
            }

            initMetroCenter();
            initMetroSide();
            resetMetroCenterNavigate();
        });
    };

    $.fn.metro.onPrevClick = function (index) {
        return true;
    };
    $.fn.metro.onNextClick = function (index) {
        return true;
    };
    $.fn.metro.showBottomSide = function ($metro, msg) {
        $('.metro_side.bottom .metro_side_content', $metro).html(msg);
        $('.metro_side.bottom', $metro).slideDown(300);
    };
})(jQuery);